import {Injectable} from '@angular/core';
import {RequestUtil} from "../../../util/request-util";
import {API} from "../../../constants/constants";
import {Observable} from "rxjs";
import {MsgResult} from "../../../models/msg-result";

@Injectable()
export class RolesService {

  constructor(private requestUtil: RequestUtil) {
  }

  /**
   * 无参获取角色
   * @returns {Observable<MsgResult>|any}
   */
  getRole(): Observable<MsgResult> {
    return this.requestUtil.get(API.getRole);
  }

  insertRole(role): Observable<MsgResult> {
    let params = "roleName=" + role.roleName + "&description=" + role.description;
    return this.requestUtil.post(API.insertRole, params);
  }

  deleteRole(roleId) {
    let params = "roleId=" + roleId;
    return this.requestUtil.post(API.deleteRole, params);
  }

  delRoles(arrId) {
    let params = "roleIdStr=" + arrId;
    return this.requestUtil.post(API.delRoles, params);
  }

  getRoleById(roleId) {
    let params = "roleId=" + roleId;
    return this.requestUtil.get(API.getRoleById, params);
  }

  updateRole(role) {
    let params = "roleId=" + role.roleId + "&roleName=" + role.roleName + "&description=" + role.description;
    return this.requestUtil.post(API.updateRole, params);
  }

  /**
   * 根据id初始化角色授权菜单树
   * @param roleId 角色id；
   * @returns {Observable<MsgResult>|any}
   */
  doAssignAuthority(roleId): Observable<MsgResult> {
    let params = "roleId=" + roleId;
    return this.requestUtil.post(API.doAssignAuthority, params);
  }

  /**
   * 角色授权（为角色分配权限）
   * 前台传递过来类似于authid1|authconfigId1,authid2|authconfigId2的字符串且起名为authIdArr
   * @param role
   * @returns {Observable<MsgResult>|any}
   */
  saveAssignAuthority(roleId, authorityIds): Observable<MsgResult> {
    let authIdArr = authorityIds;
    let params = "roleId=" + roleId + "&authIdArr=" + authIdArr;
    return this.requestUtil.post(API.saveAssignAuthority, params);
  }

  /**
   * 角色分配账户（为角色分配权限）
   * 前台传递过来类似于authid1|authconfigId1,authid2|authconfigId2的字符串且起名为authIdArr
   * @param role
   * @returns {Observable<MsgResult>|any}
   */
  assignAccount(id, accountIds): Observable<MsgResult> {
    let params = "accountIds=" + accountIds;
    return this.requestUtil.post(API.assignAccount + id + "/account/assgin?format=json", params);
  }

  /**
   * 角色分配账户（为角色分配权限）
   * 前台传递过来类似于authid1|authconfigId1,authid2|authconfigId2的字符串且起名为authIdArr
   * @param role
   * @returns {Observable<MsgResult>|any}
   */
  getAssignedAccount(roleId): Observable<MsgResult> {
    return this.requestUtil.post(API.getAssignedAccount + roleId + "/account/assigned?format=json");
  }

}
